//加载单个模型文件
import { EventDispatcher } from "three";
import { gltfLoader } from "./LoaderConfig";
class LoaderModel extends EventDispatcher {
  sTime: number = 0;
  eTime: number = 0;
  constructor() {
    super();
  }
  load(url: string, basePath = "./model/"):this {
    this.dispatchEvent({
      type: "loadStart",
      data: {},
    });
    this.sTime = new Date().getTime();
    gltfLoader
      .loadAsync(basePath + url)
      .then((res) => {
        this.eTime = new Date().getTime();
        let time: number = (this.eTime as number) - this.sTime;
        this.dispatchEvent({
          type: "loaded",
          data: {
            data: res,
            time,
            url,
          },
        });
      })
      .catch((error) => {
        this.dispatchEvent({
          type: "error",
          data: {
            data: {},
            error,
          },
        });
      });
      return this;
  }
}

export const defaultLoaderModel = new LoaderModel();
